from selenium.webdriver.common.by import By
from pages.base_page import Page


class SecureFormPage(Page):
    ele_title = (By.ID, u'page-secureForm-title')
    ele_new_secure_form_button = (By.ID, u'table-secureForm-button-new')
    ele_secure_forms_table = (By.XPATH, u'//thead/tr')

    ele_name = (By.ID, u'form-secureForm-input-name')
    ele_save_button = (By.XPATH, u"//span[text()='Save']")
    ele_delete_button = (By.XPATH, u"//tr[@id='drag-0']/td[@data-tag='operation']/div/button[2]")

    ele_error = (By.XPATH, u'//div[@role="alert"]')

    # No feature
    ele_no_feature_title = (By.ID, u'undefined-title')
    ele_no_feature_image = (By.XPATH, u"//h3[text()='Feature Introduction']")
    ele_unlock_feature_button = (By.XPATH, u"//span[text()='Unlock This Feature']")

    def __init__(self, driver, url, site_id):
        Page.__init__(self, driver, url)
        self.site_id = site_id
        self.url = url

    def check_page_has_error(self):
        try:
            flag = self.find_element(*self.ele_error).is_enabled()
            if flag:
                raise Exception("current page {} may has error".format(self.url))
        except Exception as e:
            return True

    def secure_forms_eles_check_when_has_feature(self):
        self.find_element(*self.ele_title).is_enabled()
        self.find_element(*self.ele_new_secure_form_button).is_enabled()
        self.find_element(*self.ele_secure_forms_table).is_enabled()
        self.click(self.ele_new_secure_form_button)
        # self.find_element(*self.ele_name)
        self.find_element(*self.ele_save_button)
        # self.find_element(*self.ele_delete_button)

    def secure_forms_eles_check_when_no_feature(self):
        self.find_element(*self.ele_no_feature_title).is_enabled()
        self.find_element(*self.ele_no_feature_image).is_enabled()
        self.find_element(*self.ele_unlock_feature_button).is_enabled()
